Imports System.ComponentModel
Imports System.Configuration.Install
Imports System.Security

<RunInstaller(True)> Public Class SecurityCustomAction
    Inherits System.Configuration.Install.Installer

#Region " Component Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Installer overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Component Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Component Designer
    'It can be modified using the Component Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container
    End Sub

#End Region
    Private Function SetupPolicy(ByVal sPath As String, ByVal sName As String) As Object

        'Get a reference to the User level "All Code" group.
        Dim PolLevel As Policy.PolicyLevel = Nothing
        Dim PolLevels As IEnumerator = _
            System.Security.SecurityManager.PolicyHierarchy
        Do While PolLevels.MoveNext
            If PolLevels.Current().Label = "User" Then
                PolLevel = PolLevels.Current
                Exit Do
            End If
        Loop
        Dim AllCodeCG As Policy.UnionCodeGroup
        AllCodeCG = PolLevel.RootCodeGroup  'All code group

        ' Create a new code group with the FullTrust permission
        ' set and a URL as evidence of membership.
        Dim permSet As System.Security.PermissionSet = _
            PolLevel.GetNamedPermissionSet("FullTrust")
        Dim URLMemCond As System.Security.Policy.UrlMembershipCondition = _
            New System.Security.Policy.UrlMembershipCondition(sPath)
        Dim cg As System.Security.Policy.UnionCodeGroup = _
            New System.Security.Policy.UnionCodeGroup( _
            URLMemCond, New System.Security.Policy.PolicyStatement(permSet))
        cg.Name = sName
        AllCodeCG.AddChild(cg)

        ' Save the policy.
        System.Security.SecurityManager.SavePolicy()

    End Function

    Public Overrides Sub Install(ByVal stateSaver _
        As System.Collections.IDictionary)
        MyBase.Install(stateSaver)
        SetupPolicy(Me.Context.Parameters.Item("path"), _
            Me.Context.Parameters.Item("name"))
    End Sub
    Private Function RemovePolicy(ByVal sPath As String, ByVal sName As String) As Object
        ' Get a reference to the User level "All Code" group.
        Dim PolLevel As Policy.PolicyLevel = Nothing
        Dim PolLevels As IEnumerator = _
            System.Security.SecurityManager.PolicyHierarchy
        Do While PolLevels.MoveNext
            If PolLevels.Current().Label = "User" Then
                PolLevel = PolLevels.Current
                Exit Do
            End If
        Loop
        Dim AllCodeCG As Policy.UnionCodeGroup
        AllCodeCG = PolLevel.RootCodeGroup  'All code group

        ' Determine the resolved membership condition for the 
        ' specified path.
        Dim sResolvedPath As String = _
            New System.Security.Policy.UrlMembershipCondition _
            (sPath).ToString()

        ' Locate a code group with the same name, the same resolved 
        ' path, and FullTrust; if found, remove the child group.
        Dim CGs As IEnumerator = AllCodeCG.Children.GetEnumerator()
        Do While CGs.MoveNext
            Dim CG As Policy.CodeGroup
            CG = (CType(CGs.Current(), Policy.CodeGroup))
            If CG.Name = sName Then
                If CG.MembershipCondition.ToString = sResolvedPath And _
                    AllCodeCG.PermissionSetName = "FullTrust" Then
                    AllCodeCG.RemoveChild(CG)
                    System.Security.SecurityManager.SavePolicy()
                    Exit Do
                End If
            End If
        Loop

    End Function
    Public Overrides Sub Uninstall(ByVal savedState As System.Collections.IDictionary)
        MyBase.Uninstall(savedState)
        RemovePolicy(Me.Context.Parameters.Item("path"), _
            Me.Context.Parameters.Item("name"))
    End Sub

End Class
